//
//  MineAccountSecurityViewController.m
//  ShowYork
//
//  Created by hxh on 2020/8/26.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineAccountSecurityViewController.h"
#import "MineLoginDeleteViewController.h"
#import "MineAccountSecurityCell.h"
@interface MineAccountSecurityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,copy)NSArray *titleArray;
@property (nonatomic,strong)UIView *line;
@end

@implementation MineAccountSecurityViewController

- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"用户协议",@"隐私政策",@"注销帐号"];
    }
    return _titleArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = YES;
  
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
}

- (void)setUI{
    
    self.navigationItem.title = @"隐私与安全";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = RGBA(240, 240, 240, 1);
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.right.equalTo(self.view.mas_right);
            make.left.equalTo(self.view.mas_left);
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];

    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0.5);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineAccountSecurityCell *cell = [MineAccountSecurityCell cellWithTableView:tableView];
    cell.titleLb.text = self.titleArray[indexPath.row];
    cell.lineView.hidden = self.titleArray.count - 1 == indexPath.row;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RPWebViewController *webVC = [[RPWebViewController alloc]init];
    webVC.isDark = YES;
    if (indexPath.row == 0) {
        webVC.title = @"用户协议";
        webVC.webUrl = [NSString stringWithFormat:@"%@user/agreement?type=serviceAgreement&navbar=no",HtmlUrl];
        [self.navigationController pushViewController:webVC animated:YES];
    }
    if (indexPath.row == 1) {
        webVC.title = @"隐私政策";
        webVC.webUrl = [NSString stringWithFormat:@"%@user/agreement?type=userPrivacy&navbar=no",HtmlUrl];
        [self.navigationController pushViewController:webVC animated:YES];
    }
    if (indexPath.row == 2) {
        MineLoginDeleteViewController *loginDeleteVC = [[MineLoginDeleteViewController alloc]init];
        [self.navigationController pushViewController:loginDeleteVC animated:YES];
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
@end
